package org.wiztools.te.plugin;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.wiztools.te.TEMod;
import org.wiztools.te.TEProcess;

public class XSLTransform implements TEProcess{
	private TEMod tem;
	private JFileChooser jfc;
	private JTextField jtf;
	private JTextArea jta;
	private JButton jb_browse, jb_transform;
	private Container ct;
	
	public void setTEMod(TEMod tem){
		this.tem = tem;
	}
	
	public void init(){
		jfc = new JFileChooser(System.getProperty("user.home"));
		
		ct = new JPanel();
		ct.setLayout(new BorderLayout());
		
		JPanel jp_north = new JPanel();
		jp_north.setLayout(new FlowLayout());
		jtf = new JTextField(25);
		jp_north.add(jtf);
		jb_browse = new JButton("ChooseXSLT!");
		jb_browse.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					int choice = jfc.showOpenDialog(tem.getJD());
					if(choice==JFileChooser.CANCEL_OPTION){
						return;
					}
					else if(choice==JFileChooser.ERROR_OPTION){
						return;
					}
					File f = jfc.getSelectedFile();
					jtf.setText(f.getAbsolutePath());
				}
			}
		);
		jp_north.add(jb_browse);
		jb_transform = new JButton("Transform!");
		jp_north.add(jb_transform);
		jb_transform.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					transform();
				}
			}
		);

		ct.add(jp_north, BorderLayout.NORTH);

		jta = new JTextArea();
		JScrollPane jsp = new JScrollPane(jta);
		jsp.setPreferredSize(new Dimension(50,250));
		ct.add(jsp, BorderLayout.CENTER);
	}

	private void transform(){
		String xsl = jtf.getText().trim();
		String xml_text = tem.getText().trim();
		if(xsl.equals("")){
			JOptionPane.showMessageDialog(tem.getJD(), "XSLTransform Error: Please Enter A XSL File Name!", "XSLTransform Error", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		if(xml_text.equals("")){
			JOptionPane.showMessageDialog(tem.getJD(), "XSLTransform Error: Nothing in XML buffer!", "XSLTransform Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		StringReader xml = new StringReader(tem.getText());
		StringWriter sw = new StringWriter();
		try{
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer t = tf.newTransformer(new StreamSource(xsl));
			t.transform(new StreamSource(xml), new StreamResult(sw));
			jta.setText(sw.toString());
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(tem.getJD(), "XSLTError: "+e, "XSLTError", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void action(){
		tem.dispDialog(ct, "XSLTransform Current Buffer");
	}
	
	public void destroy(){
		// do nothing!
	}
}
